package com.orientation.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orientation.commom.exception.CustomException;
import com.orientation.entity.Goods;
import com.orientation.entity.Receive;
import com.orientation.entity.Student;
import com.orientation.mapper.GoodsMapper;
import com.orientation.service.GoodsService;
import com.orientation.service.ReceiveService;
import com.orientation.service.StudentService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 物品管理表 服务实现类
 * </p>
 *
 * @author xuandian
 * @since 2022-01-09
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private GoodsService goodsService;
    @Resource
    private FilesService filesService;
    @Resource
    private ReceiveService receiveService;
    @Resource
    private StudentService sudentService;


    @Override
    public void insertGoods(MultipartFile[] files, String goodsName, String goodsType) {
        if (!StringUtils.hasLength(goodsName)) {
            throw new CustomException("18", "物品名必填");
        }
        Goods one = goodsService.getOne(Wrappers.<Goods>lambdaQuery().eq(Goods::getGoodsName, goodsName));
        if (one != null) {
            throw new CustomException("18", "此物品已经存在，请不要重复添加");
        }
        Goods goods = new Goods();
        goods.setGoodsName(goodsName);
        goods.setGoodsType(goodsType);
        //文件上传
        if (files != null && files.length > 0) {
            List<String> stringUrls = filesService.getStringUrls(files);
            if (!CollectionUtils.isEmpty(stringUrls)) {
                goods.setGoodsUrl(stringUrls == null ? null : org.apache.commons.lang3.StringUtils.join(stringUrls, ","));
            }
        }else {
            throw new CustomException("18","物资样例图必填");
        }
        this.save(goods);
    }

    @Override
    public void updateGoods(MultipartFile[] files, String sid, List<String> urls, String goodsName, String goodsType) {
        Goods goods = new Goods();

        String goodsUrl = "";
        if (CollectionUtils.isEmpty(urls)) {
            goods.setGoodsUrl(null);
        } else {
            List<String> strings = urls.stream().distinct().collect(toList());
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : strings) {
                stringBuffer.append(s);
                stringBuffer.append(",");
            }
            goodsUrl = stringBuffer.toString().substring(0, stringBuffer.length() - 1);
        }
        if (files != null && files.length > 0) {
            List<String> stringUrls = filesService.getStringUrls(files);
            if (!CollectionUtils.isEmpty(stringUrls)) {
                goods.setGoodsUrl(stringUrls == null ? null : org.apache.commons.lang3.StringUtils.join(stringUrls, ","));
            }
        }
        if (StringUtils.hasLength(goodsUrl) && StringUtils.hasLength(goods.getGoodsUrl())) {
            goods.setGoodsUrl(goodsUrl + "," + goods.getGoodsUrl());
        }
        if (StringUtils.hasLength(goodsUrl) && !StringUtils.hasLength(goods.getGoodsUrl())) {
            goods.setGoodsUrl(goodsUrl);
        }
        goods.setSid(sid);
        goods.setGoodsName(goodsName);
        goods.setGoodsType(goodsType);
        updateById(goods);
    }

    @Override
    public List<Goods> queryGoods(String studentId) {
        Student student =sudentService.getOne(Wrappers.<Student> lambdaQuery().eq(Student ::getStudentId,studentId));
        if (ObjectUtils.isEmpty(student) ) {
            throw new CustomException("18","学号输入错误");
        }
        //已经申领过的
        List<Receive> receives = receiveService.list(Wrappers.<Receive>lambdaQuery().eq(StringUtils.hasLength(studentId), Receive::getStudentId, studentId));
        List<Goods> goods = goodsService.list();
        if (!CollectionUtils.isEmpty(receives)) {
            // 取差集
            goods = goods.stream().filter(
                    m -> !receives.stream().map(Receive::getGoodsId).collect(toList()).contains(m.getSid())).collect(toList());
        }
        return goods;
    }
}
